﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLUNZ.Business;

namespace NLUNZ.Register
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.errorMessage.Visible = false;
            this.confirmationBox.Visible = false;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            if(ValidationControls())
            {
                string username = txtUsername.Text;
                string password = txtPassword.Text;
                string email = txtEmail.Text;
                Repository.Register(username, password, email);
                this.registerBox.Visible = false;
                this.confirmationBox.Visible = true;
            }
        }

        private bool ValidationControls()
        {
            List<string> errors = new List<string>();
            string username = txtUsername.Text;
            if(username.Trim().Length==0)
                errors.Add("Username is empty.");
            if(!Repository.IsUserNameAvailable(username))
                errors.Add("User name is not available.");
            string password = txtPassword.Text;
            if(password.Trim().Length ==0)
                errors.Add("Password is emtpy.");
            string email = txtEmail.Text;
            if(email.Trim().Length ==0)
                errors.Add("Email is emtpy.");

            string output = "<ul>";
            foreach (string error in errors)
            {
                output += string.Format("<li>{0}</li>", error);
            }
            output += "</ul>";
            this.errorMessage.InnerHtml = output;
            if (errors.Count == 0)
                return true;
            this.errorMessage.Visible = true;   
            return false;
        }
    }
}